package com.ghost.alibabaghost.utils;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static java.lang.System.getProperty;
import static java.lang.Character.isSpaceChar;

/**
 * 字符串处理
 *
 * @author 郑灵宇
 * @date 2017-09-06
 */
public class StringUtils {

    public static final String LINE = getProperty("line.separator");

    /**
     * 判断是空字符串
     */
    public static boolean isEmpty(String value) {
        return value == null || value.isEmpty();
    }

    /**
     * 判断不是空字符串
     */
    public static boolean isNotEmpty(String value) {
        return !isEmpty(value);
    }

    /**
     * 去掉空格
     */
    public static String trim(String value) {
        if (isEmpty(value)) {
            return value;
        }
        return isSpaceChar(value.charAt(0)) || isSpaceChar(value.length() - 1) ? value.trim() : value;
    }

    /**
     * 将List拼装成以，号分割的字符串
     */
    public static <T> String assembly(List<T> list, String limit) {
        return list == null || list.isEmpty() ? null : assembly(list.iterator(), limit);
    }

    /**
     * 将Set拼装成以，号分割的字符串
     */
    public static <T> String assembly(Set<T> set, String limit) {
        return set == null || set.isEmpty() ? null : assembly(set.iterator(), limit);
    }

    public static <T> String assembly(Iterator<T> it, String limit) {
        StringBuilder sb = new StringBuilder();
        while (it.hasNext()) {
            sb.append(it.next());
            sb = it.hasNext() ? sb.append(limit) : sb;
        }
        return sb.toString();
    }
}
